﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace WindowsFormsApplication2
{

    static class Multi
    {
        
        
        //always receive information
        public static void receive()
        {
            // Socket Creation
            Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            // Bind to port 50002
            listenSocket.Bind(new IPEndPoint(IPAddress.Any, 50002));
            // Ask to receive from a specific Multicast IP Address
            listenSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.5.42.47")));
            Console.WriteLine("Receiver is running");
            byte[] buffer = new byte[256];
            int recCount = 0;

            do
            {
                recCount = listenSocket.Receive(buffer);//every thing that receive is set on buffer (We must try to receive objects)
                Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, recCount));
                Thread.Sleep(1000);
            } while (true);
        }

        public static void send(Game game)
        {
            Console.WriteLine("Sender is running");
            // Socket Creation
            Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // Target to send the datas
            EndPoint multicastEndPoint = new IPEndPoint(IPAddress.Parse("239.5.42.47"), 50002);

            //ConsoleKeyInfo key;
            
            
            LinkedList<int[]> snakeParts= new LinkedList<int[]>();
            LinkedListNode<SnakePart> node = game.getSnake().First;
            while(node!=null){
                snakeParts.AddLast(new int[3]{node.Value.getX(),node.Value.getY(),node.Value.getDirection()}); 
                node=node.Next;
            }

            LinkedList<int[]> walls= new LinkedList<int[]>();
            LinkedListNode<SnakePart> nodeWall = game.getSnake().First;
            while(nodeWall!=null){
                walls.AddLast(new int[3]{nodeWall.Value.getX(),nodeWall.Value.getY(),nodeWall.Value.getDirection()}); 
                nodeWall=nodeWall.Next;
            }

            Serializer serializerClass = new Serializer(game.getScore(), game.getSnakesize(), new int[2] { game.getApple().getX(), game.getApple().getY() }, snakeParts, walls);
            Serializer.Serialize();


            string xmlString;
            System.Xml.Serialization.XmlSerializer x;
            do
            {
                

                System.IO.StreamWriter varToSend = serializerClass.getFile();
                xmlString = serializerClass.getFile().ToString();
                sendSocket.SendTo(Encoding.UTF8.GetBytes(serializerClass.getFile().ToString()), serializerClass.getFile().ToString().Length, SocketFlags.None, multicastEndPoint);
                Thread.Sleep(1000);
                
            } while (true);
        }
    }
}

